﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace HashCalculator.ViewModels
{
    class NotifyObject : INotifyPropertyChanged
    {
        protected string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
        {
            if (propertyExpression == null)
            {
                throw new ArgumentNullException("propertyExpression");
            }
            var member = propertyExpression.Body as MemberExpression;
            if (member == null)
            {
                throw new ArgumentException("Invalid propertyExpression");
            }
            return member.Member.Name;
        }

        protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
        {
            RaisePropertyChanged(ExtractPropertyName(propertyExpression));
        }

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
